package com.qishanor.system.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qishanor.framework.util.R;
import com.qishanor.system.model.SysMenu;
import com.qishanor.system.model.SysRoleMenu;
import com.qishanor.system.model.SysUserRole;
import com.qishanor.system.service.SysMenuService;
import com.qishanor.system.service.SysRoleMenuService;
import com.qishanor.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @description: 菜单管理
 * @author: 周振林
 * @date: 2022-04-09
 * @Copyright: 博客：http://www.zhouzhenlin.com - 沉淀、分享、成长、让自己和他人都有所收获
 **/

@RestController
@RequestMapping("/api/sys/menu")
public class SysMenuController {

    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysMenuService sysMenuService;

    /**
     * 返回当前用户的树形菜单集合
     * @param type 类型
     * @param parentId 父节点ID
     * @return 当前用户的树形菜单
     */
    @GetMapping("/user")
    @SaCheckLogin
    public Object getUserMenu(String type, Integer parentId){
        //查询用户所具有的所有角色ID
        List<Integer> roleIds=sysUserRoleService.listObjs(Wrappers.lambdaQuery(SysUserRole.class).select(SysUserRole::getRoleId).eq(SysUserRole::getUserId, StpUtil.getLoginIdAsInt()))
                .stream().map(u->(Integer)u).collect(Collectors.toList());
        //查询所有菜单ID
        List<Integer> menuIds=sysRoleMenuService.listObjs(Wrappers.lambdaQuery(SysRoleMenu.class).select(SysRoleMenu::getMenuId)
                .in(SysRoleMenu::getRoleId,roleIds)).stream().map(u->(Integer)u).collect(Collectors.toList());

        Set<SysMenu> all= CollectionUtil.newHashSet(sysMenuService.listByIds(menuIds));

        return R.ok(sysMenuService.filterMenu(all,type,parentId));
    }

    /**
     * 返回角色的菜单集合
     * @param roleId 角色ID
     * @return 属性集合
     */
    @GetMapping("/role/{roleId}")
    public R getMenuByRoleId(@PathVariable Integer roleId) {

        List<Integer> list= sysRoleMenuService.list(Wrappers.lambdaQuery(SysRoleMenu.class).eq(SysRoleMenu::getRoleId,roleId))
                .stream().map(u->u.getMenuId()).collect(Collectors.toList());
        return R.ok(list);
    }

    /**
     * 获取所有的菜单树
     * 如果parentId为空或lazy=false 则获取全部的菜单
     * 否则根据parentId加载
     */
    @GetMapping("/tree")
    @SaCheckPermission("sys_menu_view")
    public R getMenuTree(boolean lazy,Integer parentId){
        return R.ok(sysMenuService.getMenuTree(lazy,parentId));
    }


    @GetMapping("/{id}")
    public R getById(@PathVariable String id){
        return R.ok(sysMenuService.getById(id));
    }

    @PostMapping
    @SaCheckPermission("sys_menu_add")
    public R save(@RequestBody SysMenu sysMenu){
        return R.ok(sysMenuService.save(sysMenu));
    }

    @PutMapping
    @SaCheckPermission("sys_menu_edit")
    public R update(@RequestBody SysMenu sysMenu){
        return R.ok(sysMenuService.updateById(sysMenu));
    }
    @DeleteMapping("/{id}")
    @SaCheckPermission("sys_menu_del")
    public R removeById(@PathVariable String id){
        return R.ok(sysMenuService.removeById(id));
    }

}
